/*
 * kylin-os-manager
 *
 * Copyright (C) 2022, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "cleartrash.h"
#include <QDir>
#include <QDebug>

namespace KylinRubbishClear
{

ClearTrash::ClearTrash(QObject *parent) : QObject(parent) {}
void ClearTrash::checkButtonTrashEmpty()
{
    QString trash_path;
    trash_path = QDir::homePath() + "/.local/share/Trash";
    deleteContents(trash_path);
}
QStringList ClearTrash::scanTrash()
{
    mListTrash.clear();

    QString trash_path;
    trash_path = QDir::homePath() + "/.local/share/Trash";
    QDir dir(trash_path);
    if (!dir.exists()) {
        qDebug() << "ClearTrash::deleteContents 回收站路径不存在！";
        return mListTrash;
    }
    QFileInfoList fileList = dir.entryInfoList(
        QDir::Files | QDir::Dirs | QDir::Readable | QDir::Writable | QDir::Hidden | QDir::NoDotAndDotDot, QDir::Name);
    if (fileList.size() > 0) {
        QFileInfo curFile;
        QString str = "files";
        for (int i = 0; i < fileList.size(); i++) {
            curFile = fileList[i];
            qDebug() << "ClearTrash::scanTrash" << curFile.fileName();
            if (curFile.fileName() == str) {
                mListTrash = getFileList(curFile.filePath());
            }
        }
    }
    return mListTrash;
}
QStringList ClearTrash::scanTrashFiles()
{
    mListTrashFiles.clear();

    QString trash_path;
    trash_path = QDir::homePath() + "/.local/share/Trash/files";
    QDir dir(trash_path);
    if (!dir.exists()) {
        qDebug() << "ClearTrash::scanTrashFiles 回收站路径不存在！";
        return mListTrashFiles;
    }
    QFileInfoList fileList = dir.entryInfoList(
        QDir::Files | QDir::Dirs | QDir::Readable | QDir::Writable | QDir::Hidden | QDir::NoDotAndDotDot, QDir::Name);
    if (fileList.size() > 0) {
        QFileInfo curFile;
        quint64 sizeFile = 0;
        for (int i = 0; i < fileList.size(); i++) {
            curFile = fileList[i];
            if (curFile.isDir()) {
                sizeFile += getDirSize(curFile.filePath());
            } else {
                sizeFile += curFile.size();
            }
        }
        QString info = QString("Trash%1:%2").arg(trash_path).arg(sizeFile);
        mListTrashFiles.append(info);
    }
    return mListTrashFiles;
}

QStringList ClearTrash::getFileList(const QString path)
{
    QStringList fileNameList;
    fileNameList.clear();

    QDir dir(path);
    if (!dir.exists()) {
        qDebug() << "ClearTrash::getFileList 文件路径不存在！";
        return fileNameList;
    }
    QFileInfoList fileList = dir.entryInfoList(
        QDir::Files | QDir::Dirs | QDir::Readable | QDir::Writable | QDir::Hidden | QDir::NoDotAndDotDot, QDir::Name);
    if (fileList.size() > 0) {
        for (int i = 0; i < fileList.size(); i++) {
            QFileInfo curFile = fileList[i];
            QString filePath = curFile.filePath();
            fileNameList.append(filePath);
        }
    }
    return fileNameList;
}

void ClearTrash::deleteContents(const QString path)
{

    QDir dir(path);
    QFileInfoList fileList;
    QFileInfo curFile;
    if (!dir.exists()) {
        qDebug() << "ClearTrash::deleteContents 回收站路径不存在！";
        return;
    }
    fileList = dir.entryInfoList(
        QDir::Files | QDir::Dirs | QDir::Readable | QDir::Writable | QDir::Hidden | QDir::NoDotAndDotDot, QDir::Name);
    while (fileList.size() > 0) {
        qDebug() << "fileList.size():" << fileList.size();
        int infoNum = fileList.size();
        for (int i = infoNum - 1; i >= 0; i--) {
            qDebug() << "for循环：i :" << i;
            curFile = fileList[i];
            qDebug() << "ClearTrash::deleteContents :" << curFile.filePath() << curFile.size();
            getFileInfoMsg(curFile.filePath());
            if (curFile.isFile()) {
                QFile fileTemp(curFile.filePath());
                fileTemp.remove();
            }
            if (curFile.isDir()) {
                QDir dirTemp(curFile.filePath());
                dirTemp.removeRecursively();
            }
            fileList.removeAt(i);
        }
    }
}

void ClearTrash::getFileInfoMsg(const QString path)
{
    QDir dir(path);
    QFileInfoList fileList;
    QFileInfo curFile;
    if (!dir.exists()) {
        qDebug() << "ClearTrash::getFileInfoMsg 文件路径不存在！";
        return;
    }
    fileList = dir.entryInfoList(
        QDir::Files | QDir::Dirs | QDir::Readable | QDir::Writable | QDir::Hidden | QDir::NoDotAndDotDot, QDir::Name);
    while (fileList.size() > 0) {
        qDebug() << "ClearTrash::getFileInfoMsg fileList.size():" << fileList.size();
        int infoNum = fileList.size();
        for (int i = infoNum - 1; i >= 0; i--) {
            qDebug() << "ClearTrash::getFileInfoMsg for循环：i :" << i;
            curFile = fileList[i];
            qDebug() << "ClearTrash::getFileInfoMsg :" << curFile.filePath() << curFile.size();
            fileList.removeAt(i);
        }
    }
}

quint64 ClearTrash::getDirSize(const QString path)
{
    QDir dir(path);
    quint64 size = 0;
    // dir.entryInfoList(QDir::Files)返回文件信息
    foreach (QFileInfo fileInfo, dir.entryInfoList(QDir::Files)) {
        //计算文件大小
        size += fileInfo.size();
    }
    // dir.entryList(QDir::Dirs|QDir::NoDotAndDotDot)返回所有子目录，并进行过滤
    foreach (QString subDir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
        //若存在子目录，则递归调用dirFileSize()函数
        size += getDirSize(path + QDir::separator() + subDir);
    }
    return size;
}

} // namespace KylinRubbishClear
